using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace ForumClient
{
    public enum ForumLinkType
    {
        Unknown,
        Group,
        Forum,
        Post,
        User
    }
   
    public class Parser
    {
        public void ParseForumList(HtmlDocument doc, DataTable groups, DataTable forums, string sitename)
        {
            DataRow lastgrouprow = null;
            DataRow forumrow;

            DocumentMetaData fmd = new DocumentMetaData();
            fmd.Parse(doc);

            HtmlElementCollection tags = doc.GetElementsByTagName(@"a");

            foreach (HtmlElement element in tags)
            {
                string href = element.GetAttribute(@"href");

                if (null != href && href.Length > 0)
                {
                    Uri uri = new Uri(href);
                    Dictionary<string, string> pairs = DecodeQuery(uri.Query);

                    switch (GetForumLinkType(uri, pairs, sitename))
                    {
                        case ForumLinkType.Group:
                            {
                                if (null == lastgrouprow || (int)lastgrouprow["GroupID"] != Convert.ToInt32(pairs["ForumGroupID"]))
                                {
                                    lastgrouprow = groups.NewRow();
                                    lastgrouprow["GroupID"] = Convert.ToInt32(pairs["ForumGroupID"]);
                                    lastgrouprow["GroupName"] = element.InnerText;
                                    lastgrouprow["SiteID"] = fmd.SiteID;
                                    groups.Rows.Add(lastgrouprow);
                                }
                                break;
                            }
                        case ForumLinkType.Forum:
                            {
                                forumrow = forums.NewRow();
                                forumrow["ForumID"] = Convert.ToInt32(pairs["ForumID"]);
                                forumrow["ForumName"] = element.InnerText;
                                forumrow["GroupID"] = lastgrouprow["GroupID"];
                                forums.Rows.Add(forumrow);
                                break;
                            }
                        default:
                            {
                                break;
                            }
                     }
                }
            }
        }

       private ForumLinkType GetForumLinkType(Uri uri, Dictionary<string, string> uriquerypairs, string sitename)
       {
           if (0 == String.Compare(String.Format("/{0}/default.aspx", sitename), uri.LocalPath, true) &&
               true == uriquerypairs.ContainsKey(@"ForumGroupID"))
           {
               return ForumLinkType.Group;
           }
           else if (0 == String.Compare(String.Format("/{0}/ShowForum.aspx", sitename), uri.LocalPath, true) &&
                    true == uriquerypairs.ContainsKey(@"ForumID"))
           {
               return ForumLinkType.Forum;
           }

           return ForumLinkType.Unknown;
       }

       private Dictionary<string, string> DecodeQuery(string query)
       {
           Dictionary<string, string> querypairs = new Dictionary<string, string>();
           if (null == query || 0 == query.Length)
           {
               return querypairs;
           }
           // Remove ? in front
           query = query.Substring(1);
           string[] pairs = query.Split("&".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
           string[] pairpairs;
           foreach (string pair in pairs)
           {
               pairpairs = pair.Split("=".ToCharArray());
               if (false == querypairs.ContainsKey(pairpairs[0]))
               {
                   querypairs.Add(pairpairs[0], pairpairs[1]);
               }
           }

           return querypairs;
       }

   }
}
